programming4us
           
 
 
Windows Phone

User Interface : Customizing the Soft Input Panel Keyboard to Accept Only Numbers

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/10/2011 9:26:28 AM

1. Problem

You need to provide a virtual keyboard (also called a Soft Input Panel, or SIP) that accepts only numbers so users can insert numeric values into a text field.

2. Solution

You can use the InputScope property provided by the TextBox control and use the KeyDown event to exclude keys that are not needed.

3. How It Works

When the user has to insert information in your application and taps on a text field, a SIP keyboard appears, allowing the input of text. By using the InputScope attribute in your XAML code, you can customize the SIP in order to help users during text input.

You can use one of the values available from the InputScopeNameValue enumerator, as shown in Table 3-1. For example, if you use the TelephoneNumber value, the tapped text box will show the panel used to compose a telephone number.

NOTE

There are more than 50 values in the InputScopeNameValue enumerator, and most of them either are useless or cause no changes to the SIP. Table 1 lists the most common values that are specific to the Windows Phone.

Table 1. The Most Common InputScope Enumerator Values
ValueDescription
ChatUsed for chat messaging, SIP recognizes pre-stored abbreviations.
DigitsSpecifies digits.
EmailNameOrAddressSpecifies e-mail names and addresses, because some shortcut keys such as the at character (@) and the .com key will be added to the main keyboard.
CurrencyAmountSpecifies currency amount values.
DateInserts a calendar date.
MapsSpecifies a map location.
NameOrPhoneNumberSpecifies the destination address of a Short Message Service (SMS) text message.
PasswordSpecifies password text.
TextThe standard SIP layout for inserting text.
UrlSpecifies an Internet address.

You can use a combination of the InputScope attribute and the KeyDown event handler to customize the SIP behavior.

This recipe creates a SIP layout that accepts only numeric digits. You will customized the text box with the TelephoneNumber SIP layout and disable the input of non-numeric keys.

4. The Code

To demonstrate this recipe, you have to open the 7Drum project from the companion code by using Visual Studio 2010 and then focus on the Exercises page. It contains some data describing the exercise, such as the exercise's name and duration. It is the Duration text box that has to be numeric and accept only numeric digits.

In the Exercise.xaml file, you define the text box fields that compose the input form to specify an exercise. Note that the Duration text box field has the InputScope set to the TelephoneNumber value:

. . .
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<TextBlock Grid.Row="0" Grid.Column="0"
Text="Name:" Style="{StaticResource PhoneTextNormalStyle}"
VerticalAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="1" x:Name="txtName" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Description:"
Style="{StaticResource PhoneTextNormalStyle}"
VerticalAlignment="Center" />
<TextBox Grid.Row="1" Grid.Column="1" x:Name="txtDescription"
AcceptsReturn="True" Height="300"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Duration (min):"
Style="{StaticResource PhoneTextNormalStyle}"
VerticalAlignment="Center" />
<TextBox Grid.Row="2" Grid.Column="1" x:Name="txtDuration"
InputScope="TelephoneNumber" KeyDown="txtDuration_KeyDown"/>
</Grid>
</StackPanel>
</Grid>
. . .


In the Exercise.xaml.cs code, the KeyDown event handler ensures that no numeric digits are inserted in the Duration text box. The KeyEventArgs parameter provided by the KeyDown event handler contains the Key property that represents the pressed key. You can use this property to check that the pressed key is not the space key or other non-numeric key such as # or *. In this case, you can set the Handled property to true so that the key is accepted by the Duration text box. Otherwise, you have to set the Handled property value to false so that the key is not accepted.

private void txtDuration_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Space && e.Key != Key.D8 && e.Key != Key.D3 &&
e.Key != Key.Unknown)
e.Handled = false;
else
e.Handled = true;
}

5. Usage

Press Ctrl+F5 from Visual Studio 2010 so that the application is deployed to either the Windows Phone 7 Emulator or the Windows Phone 7 device. The application starts. Select the Training menu item from the main page and then select the Add application bar button to add a new exercise.

The Exercise page loads, and you will see the text boxes shown in Figure 1.

Figure 1. The Exercise page with the Duration numeric field

Now tap the Duration text box, and the TelephoneNumber layout of the Soft Input Panel virtual keyboard appears (see Figure 2).

Figure 2. The customized TelephoneNumber virtual keyboard

Try to press numeric and non-numeric keys, and you will see that only the former are allowed.

Other -----------------
- User Interface : Detecting Changes in the Theme Template
- Developing for Windows Phone and Xbox Live : Multiplayer Games (part 6) - Searching for an Available Network Session
- Developing for Windows Phone and Xbox Live : Multiplayer Games (part 5) - Searching for an Available Network Session
- Developing for Windows Phone and Xbox Live : Multiplayer Games (part 4) - Building a Game Lobby
- Developing for Windows Phone and Xbox Live : Multiplayer Games (part 3) - Creating a Network Session
- Developing for Windows Phone and Xbox Live : Multiplayer Games (part 2) - Main Menu and State Management
- Developing for Windows Phone and Xbox Live : Multiplayer Games (part 1) - Getting Ready for Networking Development
- User Interface : Using the ApplicationBar Control
- User Interface : Creating an Animated Splash Screen
- Windows Phone 7 Game Development : The World of 3D Graphics - Vertex and Index Buffers
- Windows Phone 7 Game Development : The World of 3D Graphics - Hidden Surface Culling
- Windows Phone 7 Game Development : The World of 3D Graphics - The Depth Buffer
- Windows Phone 7 Game Development : The World of 3D Graphics - Rendering 3D Objects
- Windows Phone 7 Game Development : The World of 3D Graphics - Perspective Projection
- Developing for Windows Phone and Xbox Live : Let the 3D Rendering Start
- Developing for Windows Phone and Xbox Live : Reach and HiDef Graphics Profiles
- Developing for Windows Phone and Xbox Live : Graphics Pipeline
- Developing for Windows Phone and Xbox Live : Graphics Pipeline
- Programming Windows Phone 7 : Elements and Properties - More on Images
- Programming Windows Phone 7 : TextBlock Properties and Inlines
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us